import {Component, EventEmitter,ViewChild, Input, OnInit, Output} from '@angular/core';
import {Router} from '@angular/router';
import {HttpClient} from '@angular/common/http';
import { InitService } from '../../../public/serve/init/init.service';
import { appThemeTagUrl } from '../theme-tag.url';
import { themeTag } from '../theme-tag.data';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { Service } from '../../../server/server';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import {MessageService} from 'primeng/api';
declare var $:any;
@Component({
  selector: 'app-theme-tag-table',
  templateUrl: './theme-tag-table.component.html',
  styleUrls: ['./theme-tag-table.component.css']
})
export class ThemeTagTableComponent implements OnInit {

  total      : number = 0;
  naturalNum : number = 0;

  tabVal : Array<object>;

  isSelectAll : boolean = false;
  isChose : boolean = false;
  right: any = {
    "systemThemeTag": {},
  };
  constructor(
    private server: Service,
    public request: CudTableService,
    private tableListService: CudTableService,
    private hintsService: HintsService,
    private removeTableService: RemoveTableService,
    public initService: InitService,
  ) {
    this.right = $.extend(this.right, server.getRight());
   }

  ngOnInit() {
    themeTag.pagination.pageNumber = 1;
    themeTag.pagination.pageSize = 20;
    this.getThemeWordsLabelList();
  }

  /**
   * 主题词标签列表
   */
  getThemeWordsLabelList(){
    let data = this._tableData();
    this.request.postRequest(this.server.baseUrl +appThemeTagUrl.getThemeWordsLabelList,data).then((res:any) => {
      if (res.result_code === '0000') {
        this.tabVal = res.result;
        this.total = res.length;
        this.naturalNum = themeTag.pagination.naturalNum;
      }else{
        this.hintsService.messages(res.message,res.result_code);
      }
      this._cancelChecked();
      return;
    })
    .catch((err: any)=>{
      this.hintsService.messages("error","error");
      this._cancelChecked();
      return;
    });
  }


  /**
   * 全选
   * @param bool
   * @param tabVal
   */
  _selectAll(bool,tabVal){
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'labelId');
    if (this.isSelectAll == false) {
      this.getThemeWordsLabelList();
    }
  }

   /**
    * 单选
    * @param bool
    * @param labelId
    * @param length
    */
   _chose(bool, labelId, length){
    this.isSelectAll = this.removeTableService.removeChose(bool, labelId, length)['bool'];
   }

  /**
   * 重置表格复选框、选中的ID
   */
  private _cancelChecked(){
    this.isChose = this.isSelectAll = false;
    this.removeTableService._clearId("labelId");
  }

  //查询参数
  private _tableData(){
    return {
      token         : themeTag.retrieval.token,
      pageNo        : themeTag.pagination.pageNumber,
      pageSize      : themeTag.pagination.pageSize,
      queryCriteria : themeTag.retrieval.queryCriteria
    }
  }

    /**
     * 删除（弹窗）
     * @param info
     */
    _delete(id) {
        this.removeTableService.keywordRemoveChose(true,id,1);
        $("#delete").modal("show");
    }

}
